﻿using System;
using System.Data;
using System.Data.SQLite;
using System.IO;
using System.Linq;

namespace YandexWebmaster
{
    public partial class BD
    {
        public User GetUserBD(Account account)
        {
            User result = null;
            DataTable datatable = GetTable("sites", $"SELECT * FROM users WHERE id_account='{account.ID}'");

            if (datatable.Rows.Count > 0)
            {
                result = datatable.AsEnumerable()
                .Select(
                    t => new User
                    {
                        ID = Convert.ToInt32(t["id"]),
                        ID_account = Convert.ToInt32(t["id_account"])
                    }
                ).Single();
            }
                
            return result;
        }

        public void InsertUser(User user)
        {
            using (var connection = new SQLiteConnection($"Data Source={Directory.GetCurrentDirectory()}\\{Name};Version=3;"))
            {
                string query = $"INSERT INTO users (id, id_account) VALUES ({user.ID}, {user.ID_account})";
                connection.Open();
                SQLiteCommand command = new SQLiteCommand(connection);
                command.CommandText = query;
                command.ExecuteNonQuery();
            }
        }
    }
}
